// pages/accrent/accrent.js
var app = getApp();
var API_URL = app.globalData.API_URL;
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      var userImg = wx.getStorageSync('userImg');
      that.setData({
        userImg: userImg
      });
      //是否登录
      var uuid = parseInt(wx.getStorageSync('uuid'));
      if (uuid > 0) {
        wx.switchTab({
          url: '/pages/tsindex/tsindex'  //正确登录授权后跳转页面
        })
      }
  },
  getPhoneNumber: function (e) {
    var that = this;
    var code = wx.getStorageSync('code');
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny' || e.detail.errMsg == 'getPhoneNumber:fail:cancel to confirm login') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) {
          //
        }
      })
    } else {
      that.sLogin(code, e.detail.iv, e.detail.encryptedData);
    
    }
  },

  sLogin: function (code, iv, encryptedData) {//通用登录
   
    var that = this;
    //请求服务器
    wx.request({
      url: API_URL + 'account/login',
      method: "POST",
      data: {
        js_code: code,
        iv: iv,
        encryptedData: encryptedData
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {// success
        console.log(res);
        var result = res.data;
        var phone = result.data.phone;
        var uuid = result.data.uuid;
        var nickname = result.data.nickname;
        if (result.code == 1) {
          wx.setStorage({
            key: 'phone',
            data: phone
          });
          wx.setStorage({
            key: 'uuid',
            data: uuid
          });
          wx.setStorage({
            key: 'openid',
            data: result.data.openid
          });
          wx.setStorage({
            key: 'token',
            data: result.data.token,
          });
        }
        that.getphone(code, iv, encryptedData);
      },
      fail: function (obj) {
        //console.log(obj);
      }
    })
  },
  getphone: function (code, iv, encryptedData) {
    wx.request({
      url: API_URL + 'account/phone',
      method: "POST",
      data: {
        js_code: code,
        iv: iv,
        encryptedData: encryptedData
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {// success
        var result = res.data;
        wx.setStorage({
          key: 'uuid',
          data: result.data.uuid
        });
        wx.setStorage({
          key: 'phone',
          data: result.data.phone
        });
        wx.setStorage({
          key: 'token',
          data: result.data.token
        });
        wx.switchTab({
          url: '/pages/tsindex/tsindex',  //正确登录授权后跳转页面
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})